//
//  HOMNSArraySelectSingleWhereTests.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 10/10/06.
//  Copyright 2006 Ofri Wolfus. All rights reserved.
//

#import "HOMNSArraySelectSingleWhereTests.h"


@implementation HOMNSArraySelectSingleWhereTests

- (id)init {
	if ((self = [super init])) {
		int i;
		
		msg1 = [MSG(hasPrefix:@"b") retain];
		msg2 = [MSG(hasPrefix:@"9999") retain];
	}
	
	return self;
}

- (void)dealloc {
	[msg1 release]; msg1 = nil;
	[msg2 release]; msg2 = nil;
	[super dealloc];
}

#pragma mark -
#pragma mark Small Array Tests

- (id)testSmallArray {
	return [smallArray selectSingleWhere:MSG(hasPrefix:@"b"), nil];
}

- (id)testSmallArray_CachedMessage {
	return [smallArray selectSingleWhere:msg1, nil];
}

- (id)testSmallArray_objectAtIndex {
	unsigned i;
	unsigned c = [smallArray count];
	
	for (i = 0U; i < c; i++) {
		NSString *str = [smallArray objectAtIndex:i];
		
		if ([str hasPrefix:@"b"])
			return str;
	}
	
	return nil;
}

- (id)testSmallArray_NSEnumerator {
	NSEnumerator *e = [smallArray objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject])) {
		if ([str hasPrefix:@"b"])
			return str;
	}
	
	return nil;
}

#pragma mark -
#pragma mark Large Array Tests

- (id)testLargeArray {
	return [largeArray selectSingleWhere:MSG(hasPrefix:@"9999"), nil];
}

- (id)testLargeArray_CachedMessage {
	return [largeArray selectSingleWhere:msg2, nil];
}

- (id)testLargeArray_objectAtIndex {
	unsigned i;
	unsigned c = [largeArray count];
	
	for (i = 0U; i < c; i++) {
		NSString *str = [largeArray objectAtIndex:i];
		
		if ([str hasPrefix:@"9999"])
			return str;
	}
	
	return nil;
}

- (id)testLargeArray_NSEnumerator {
	NSEnumerator *e = [largeArray objectEnumerator];
	NSString *str;
	
	while ((str = [e nextObject])) {
		if ([str hasPrefix:@"9999"])
			return str;
	}
	
	return nil;
}

#pragma mark -
#pragma mark Tests Verification

- (BOOL)verifyResult:(NSString *)str ofTest:(SEL)sel {
	NSString *name = NSStringFromSelector(sel);
	
	if ([name hasPrefix:@"testSmallArray"]) {
		DPTestAssert([str isEqualToString:@"b"], @"The returned string is not equal to 'b'. Returned string = %@", str);
	} else if ([name hasPrefix:@"testLargeArray"]) {
		DPTestAssert([str isEqualToString:@"9999"], @"The returned string is not equal to '9999'. Returned string = %@", str);
	} else {
		return [super verifyResult:str ofTest:sel];
	}
	
	return YES;
}

@end
